// by Xeno
#define THIS_FILE "fn_x_deploymhq.sqf"
#include "x_setup.sqf"
private ["_mhq", "_depltime", "_camotype", "_camo", "_nmhq"];
if (!X_Client || {!alive player} || {isNil QGVAR(curvec_dialog)} || {isNull GVAR(curvec_dialog)} || {!alive GVAR(curvec_dialog)}) exitWith {};

_entities = (position player) nearEntities [["LandVehicle", "Air"], 10];
_nmhq = objNull;
_dd = 599;
{
	if (!isNull _x && {!(_x isKindOf "ParachuteBase")} && {!(_x isKindOf "BIS_Steerable_Parachute")}) then {
		_distt = _x distance player;
		if (_distt < _dd) then {
			_nmhq = _x;
			_dd = _distt;
		};
	};
} forEach _entities;

if (isNull _nmhq) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_1451")};

_vty = GV(_nmhq,GVAR(vec_type));
_isthere = (!isNil "_vty" && {_vty == "MHQ"});
if (_isthere && {[GVAR(base_trigger), position _nmhq] call FUNC(inTrigger)}) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_212")};

if (surfaceIsWater (position GVAR(curvec_dialog))) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_213")};

_mhq = GVAR(curvec_dialog);

_depltime = GV(_mhq,GVAR(MHQ_Depltime));
if (!isNil "_depltime" && {_depltime > time}) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_214")};

_hxhx = GV(_mhq,GVAR(MHQ_Deployed));
if (isNil "_hxhx" || {!_hxhx}) then {
	if !(_mhq call FUNC(GetVehicleEmpty)) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_215")};

	_camotype = switch (GVAR(player_side)) do {
		case blufor: {"CamoNet_OPFOR_big_F"};
		case opfor: {"CamoNet_BLUFOR_big_F"};
	};
	_pmpos = position _mhq;
	_pmpos set [2, 0];
	// TODO The camo nets in A3 are attacked by enemy AI for no reason, so don't use them!
	if (GVAR(with_mhq_camo) == 0) then {
		_camo = createVehicle [_camotype, _pmpos, [], 0, "NONE"];
		_camo allowDamage false;
		_camo setDir direction _mhq;
		_camo setVectorUp (vectorUp _mhq);
		_camo setPos _pmpos;		
		_mhq setVariable [QGVAR(MHQ_Camo), _camo, true];
	};
	_mhq setVariable [QGVAR(MHQ_Deployed), true, true];
	[QGVAR(mhqdepl), [_mhq, true]] call FUNC(NetCallEvent);
	_mhq setVariable [QGVAR(MHQ_Depltime), time + 10, true];
} else {
	_camo = GV(_mhq,GVAR(MHQ_Camo));
	if (!isNil "_camo" && {!isNull _camo}) then {deleteVehicle _camo};
	_mhq setVariable [QGVAR(MHQ_Deployed), false, true];
	[QGVAR(mhqdepl), [_mhq, false]] call FUNC(NetCallEvent);
	_mhq setVariable [QGVAR(MHQ_Depltime), time + 10, true];
};